package com.haizhi.crm.biz.task.message.engine;

import com.haizhi.crm.biz.task.message.handler.*;
import com.haizhi.crm.biz.task.message.model.ContextDto;
import com.haizhi.crm.biz.task.message.queue.Container;
import com.haizhi.crm.common.log.GLog;
import com.haizhi.crm.common.log.LogFactory;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by wangshan on 2018/5/30.
 */
@Component
public class MessagePush {
    private static final GLog LOG = LogFactory.getLogger(MessagePush.class);
    private static ExecutorService executor = Executors.newSingleThreadExecutor();

    @PostConstruct
    public void consumeMessage() {
        try {
            AbstractHandler abstractHandler = HandlerBuilder.build()
                    .add(BranchHandler.class)
                    .add(RelationHandler.class)
                    .add(ConcernHandler.class)
                    .add(PushFlagHandler.class)
                    .add(CompanyHandler.class)
                    .add(MessageSettingHandler.class)
                    .add(EsUpdateHandler.class)
                    .add(PushHandler.class)
                    .get();
            executor.submit(new Runnable() {
                @Override
                public void run() {
                    while (true) {
                        ContextDto contextDto = Container.take();
                        abstractHandler.processData(contextDto);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
            LOG.error("message center push produce error" );
        }
    }
}
